﻿using Caliburn.Micro;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.IOC.Core.Mediators;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using System.Windows;
using SlamDunk.MES.Client.ViewModels.MaterialLot;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.StockContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.MES.Presentation.Maps;
using SlamDunk.MES.Presentation.Models;
using SlamDunk.MES.Presentation.Models.NotifyModels;

namespace SlamDunk.MES.Client.ViewModels.Execution
{
    /// <summary>
    /// 工单实际投入/产出物料添加ViewModel
    /// </summary>
    public class AddActualMaterialsViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 作业管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IResourceContract> _iResourceContract;

        /// <summary>
        /// 窗口管理器
        /// </summary>
        private readonly IWindowManager _windowManager;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public AddActualMaterialsViewModel(ServiceProxy<IResourceContract> iResourceContract, IWindowManager windowManager)
        {
            this._iResourceContract = iResourceContract;
            this._windowManager = windowManager;
        }
        #endregion

        #region 属性
        #region 物料定义列表
        /// <summary>
        /// 物料定义列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<IdentiferNameNm> MaterialList { get; set; }
        #endregion

        #region 物料定义列表
        /// <summary>
        /// 物料定义列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<MaterialInfo> Materials { get; set; }
        #endregion

        #region 已选物料定义
        /// <summary>
        /// 已选物料定义
        /// </summary>
        [DependencyProperty]
        public IdentiferNameNm SelectedMaterial { get; set; }
        #endregion

        #region 物料批次名称
        /// <summary>
        /// 物料批次名称
        /// </summary>
        [DependencyProperty]
        public string MaterialLotName { get; set; }
        #endregion

        #region 物料编号
        /// <summary>
        /// 物料编号
        /// </summary>
        [DependencyProperty]
        public string MaterialNo { get; set; }
        #endregion

        #region 物料批次Id
        /// <summary>
        /// 物料批次Id
        /// </summary>
        [DependencyProperty]
        public Guid MaterialLotId { get; set; }
        #endregion

        #region 排序
        /// <summary>
        /// 排序
        /// </summary>
        [DependencyProperty]
        public int Sort { get; set; }
        #endregion

        #region 数量
        /// <summary>
        /// 数量
        /// </summary>
        [DependencyProperty]
        public decimal Quantity { get; set; }
        #endregion

        #region 描述
        /// <summary>
        /// 描述
        /// </summary>
        [DependencyProperty]
        public string Description { get; set; }
        #endregion

        #region 已选物料物料批次
        /// <summary>
        /// 已选物料物料批次
        /// </summary>
        [DependencyProperty]
        public MaterialLotInfo SelectedMaterialLot { get; set; }
        #endregion

        #region 工厂模型Id
        /// <summary>
        /// 工厂模型Id
        /// </summary>
        [DependencyProperty]
        public Guid? FacilityId { get; set; }
        #endregion
        #endregion

        #region 方法
        #region 加载 —— async Task Load(Guid? facilityId)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(Guid? facilityId)
        {
            this.FacilityId = facilityId;
            PageModel<MaterialInfo> materials = await Task.Run(() => this._iResourceContract.Channel.GetMaterialsByPage(
                null,//关键字
                null,//物料来源类型
                null,//是否按批量管理
                FacilityId,//工厂模型Id
                true,//是否启用
                null,//物料类Id
                1,
                int.MaxValue));
            Materials = new ObservableCollection<MaterialInfo>(materials.Datas);
            IEnumerable<IdentiferNameNm> materialmodels = materials.Datas.Select(x => x.ToIdentiferName());
            MaterialList = new ObservableCollection<IdentiferNameNm>(materialmodels);
        }
        #endregion

        #region 物料定义选中事件
        /// <summary>
        /// 物料定义选中事件
        /// </summary>
        public async void LoadMaterial()
        {
            if (SelectedMaterial != null)
            {
                var materialInfo = Materials.Where(x => x.Id == SelectedMaterial.Id).FirstOrDefault();
                MaterialNo = materialInfo.Number;
                //判断是否按照批次管理
                if (materialInfo.HasLot)
                {
                    SelectMaterialLotViewModel viewModel = ResolveMediator.Resolve<SelectMaterialLotViewModel>();
                    viewModel.Load(FacilityId, SelectedMaterial.Id);
                    bool? result = await this._windowManager.ShowDialogAsync(viewModel);
                    if (result == true)
                    {
                        this.SelectedMaterialLot = viewModel.SelectedMaterialLot;
                        MaterialLotName = viewModel.SelectedMaterialLot.ProductionBatchNo;
                        MaterialLotId = viewModel.SelectedMaterialLot.Id;
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择物料信息！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证
            if (SelectedMaterial == null)
            {
                MessageBox.Show("请选择物料信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            if (Quantity == 0)
            {
                MessageBox.Show("请输入双良！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            #endregion
            await base.TryCloseAsync(true);
        }
        #endregion
        #endregion
    }
}
